/*
 * Exploder
 * 
 * Tero Pyylampi (218437), Jussi Tuurinkoski (211594)
 *
 * An interface object for the editor mode which contains almost everything
 */

#include "GameInterfaceEditorContainer.h"
#include "GameInterfacePhysicObjectButton.h"
#include "../objects/physicobjects/GamePhysicObjectEngine.h"
#include "../objects/physicobjects/elements/GameConstructionElement.h"
#include "../GameCamera.h"
#include "../../editor/EditorScene.h"
#include "../../editor/EditorLayer.h"
#include "../../Common.h"
#include "../../Resources.h"

using namespace cocos2d;

// default constructor
GameInterfaceEditorContainer::GameInterfaceEditorContainer( cocos2d::CCLayer *layer, MainEngine *mainEngine, EditorScene *editorScene )
	: GameInterfaceItemAndObjectContainer( layer, mainEngine ), mEditorScene( editorScene ) {

	// create editor items
	GameInterfacePhysicObjectButton *woodBoxButton = new GameInterfacePhysicObjectButton( mLayer, this, ELEMENT_WOOD_BOX, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.77f ) );
	mObjectButtons.push_back( woodBoxButton );
	GameInterfacePhysicObjectButton *steelBoxButton = new GameInterfacePhysicObjectButton( mLayer, this, ELEMENT_STEEL_BOX, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.57f ) );
	mObjectButtons.push_back( steelBoxButton );
	GameInterfacePhysicObjectButton *steelBarButton = new GameInterfacePhysicObjectButton( mLayer, this, ELEMENT_STEEL_BAR, ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.37f ) );
	mObjectButtons.push_back( steelBarButton );

	// add header label
	cocos2d::CCLabelBMFont *headerLabel = CCLabelBMFont::labelWithString( "Editor", getResource( Fonts::FONT_NORMAL ) );
	headerLabel->setScaleX( RATEX * 0.8f );
	headerLabel->setScaleY( RATEY * 0.8f );
	headerLabel->setPosition( ccp( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.92f ) );
	mLayer->addChild( headerLabel, Z_LAYER_INTERFACE );

	// save button
	CCLabelBMFont *saveLabel1 = CCLabelBMFont::labelWithString( "Save", getResource( Fonts::FONT_NORMAL ) );
	CCMenuItemLabel *saveLabel = CCMenuItemLabel::itemWithLabel( saveLabel1, this, menu_selector( GameInterfaceEditorContainer::saveCallback ) );
	saveLabel->setPosition( WIN_WIDTH * 0.91f, WIN_HEIGHT * 0.2f );
	saveLabel->setScale( RATEX * 0.8f );
	mMenu->addChild( saveLabel, Z_LAYER_INTERFACE );
}


// default destructor
GameInterfaceEditorContainer::~GameInterfaceEditorContainer() {

}

// update function of the object
void GameInterfaceEditorContainer::update( cocos2d::ccTime dt ) {
	GameInterfaceItemAndObjectContainer::update( dt );
}

// help button
void GameInterfaceEditorContainer::helpCallback( cocos2d::CCObject *pSender ) {
	// TODO
}

// save callback
void GameInterfaceEditorContainer::saveCallback( cocos2d::CCObject *pSender ) {
	mEditorScene->saveCurrentLevel( 0 );
}

// create and return the physics body of the wanted element
b2Body *GameInterfaceEditorContainer::createNewItem( GameCamera *camera, PhysicObjectItems type, b2Vec2 pos ) {
	GameConstructionElement *e = mEditorScene->getEditorLayer()->getPhysicObjectEngine()->createElementObject( mEditorScene->getEditorLayer()->getPhysicsEngine(), mEditorScene->getEditorLayer(), camera, true, type, pos );
	return e->getBodyObjects()->at( 0 );
}
